﻿

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Collections;


namespace Lib.CommonForm
{
    public class tbl_Employees_Info : Standard_Info
    {
        public int EmployeeId { get; set; }
        public string EmployeeCode { get; set; }
        public string EmployeeName { get; set; }
        public string Address { get; set; }
        public string Email { get; set; }
        public string Phone { get; set; }
        public string Mobile { get; set; }
        public string Position { get; set; }
        public int BranchId { get; set; }
        public int DepartmentId { get; set; }
        public string CreateUser { get; set; }
        public string LastUser { get; set; }
    }
    public class tbl_Employees_Length
    {
        public static int EmployeeCode = 50;
        public static int EmployeeName = 256;
        public static int Address = 256;
        public static int Email = 50;
        public static int Phone = 50;
        public static int Mobile = 50;
        public static int Position = 50;
    }
    public class tbl_Employees_Controller
    {
        #region "Contructor"

        private static tbl_Employees_Controller _instance;

        protected tbl_Employees_Controller() { }

        protected void Dispose()
        {
            _instance = null;
        }

        private static object _syncLock = new object();

        public static tbl_Employees_Controller Instance()
        {
            if (_instance == null)
            {
                lock (_syncLock)
                {
                    if (_instance == null)
                        _instance = new tbl_Employees_Controller();
                }
            }
            return _instance;
        }

        #endregion

        public DataTable GetList(out int ItemCount, string orderBy, string orderDirection, int currentPage, int pageSize, string keyWord, int BranchID, int DepartmentID)
        {
            DataTable retVal = null;
            ItemCount = 0;
            DataSet ds = DataConnect.ExecuteDataset("prc_Employees_GetList", orderBy, orderDirection, currentPage, pageSize, keyWord, BranchID, DepartmentID);
            if (ds != null)
            {
                retVal = ds.Tables[0];
                if (ds.Tables[1] != null)
                    ItemCount = Globals.ToInt32(ds.Tables[1].Rows[0][0]);
            }
            return retVal;
        }


        public DataTable GetAll()
        {
            return DataConnect.ExecuteDatatable("prc_Employees_GetAll");
        }

        public bool Delete(int EmployeeId)
        {
            return (Globals.ToInt32(DataConnect.ExecuteNonQuery("prc_Employees_Delete", EmployeeId)) > 0);
        }

        public bool IsExist(tbl_Employees_Info i)
        {
            return (Globals.ToInt32(DataConnect.ExecuteScalar("prc_Employees_Exists", i.EmployeeId, i.EmployeeName)) > 0);
        }

        public int Save(tbl_Employees_Info i)
        {
            int retVal = 0;

            if (i.EmployeeId == 0)
                retVal = Globals.ToInt32(DataConnect.ExecuteScalar("prc_Employees_Insert", i.BranchId,
                i.DepartmentId, 
                i.EmployeeCode,
                i.EmployeeName,
                i.Address,
                i.Email,
                i.Phone,
                i.Mobile,
                i.Position,
                i.CreateUser));
            else
            {
                if (DataConnect.ExecuteNonQuery("prc_Employees_Update", i.EmployeeId,
                    i.BranchId,
                    i.DepartmentId,
                    i.EmployeeCode,
                    i.EmployeeName,
                    i.Address,
                    i.Email,
                    i.Phone,
                    i.Mobile,
                    i.Position,
                    i.LastUser) > 0)

                    retVal = i.EmployeeId;
            }
            return retVal;
        }

        public tbl_Employees_Info GetInfo(int EmployeeId)
        {
            tbl_Employees_Info retVal = null;
            DataTable dt = DataConnect.ExecuteDatatable("prc_Employees_GetInfo", EmployeeId);
            if (dt != null)
                foreach (DataRow dr in dt.Rows)
                {
                    retVal = new tbl_Employees_Info();
                    retVal.FillDataProperty(dr);
                }

            return retVal;
        }
    }
}
